package com.asha.md360player4android.vr.projection;

import android.content.Context;
import android.graphics.RectF;
import android.opengl.Matrix;

import com.asha.md360player4android.vr.objects.FYMDPlane2;
import com.asha.md360player4android.vr.objects.PlaneScaleCalculator;
import com.asha.md360player4android.vr.plugins.FYCalibratePluginEx2;
import com.asha.vrlib.MD360Director;
import com.asha.vrlib.model.MDMainPluginBuilder;
import com.asha.vrlib.model.MDPosition;
import com.asha.vrlib.objects.MDAbsObject3D;
import com.asha.vrlib.objects.MDObject3DHelper;
import com.asha.vrlib.plugins.MDAbsPlugin;

/**
 * Created by android on 7/6/17.
 */

public class FYRecordDomeProjection extends FYDomeProjection {
    public FYRecordDomeProjection(PlaneScaleCalculator calculator, RectF textureSize, float degree, boolean isUpper) {
        super(textureSize, degree, isUpper);
        planeScaleCalculator = calculator;
    }

    public static FYRecordDomeProjection create(int scaleType, RectF textureSize, float degree, boolean isUpper) {
        return new FYRecordDomeProjection(new PlaneScaleCalculator(scaleType, textureSize), textureSize, degree, isUpper);
    }

    @Override
    public void turnOnInGL(Context context) {
        object3DPlus = new FYMDPlane2(planeScaleCalculator);
        MDObject3DHelper.loadObj(context, object3DPlus);
        super.turnOnInGL(context);
    }

    @Override
    public MDAbsPlugin buildMainPlugin(MDMainPluginBuilder builder) {
        return new FYCalibratePluginEx2(builder);
    }

    /*-----------FOR PLUS------------------------------------------------*/
    private FYMDPlane2 object3DPlus;
    private PlaneScaleCalculator planeScaleCalculator;
    private static final MDPosition positionPlus = MDPosition.newInstance().setZ(-2f);

    @Override
    public MDAbsObject3D getObject3DPlus() {
        return object3DPlus;
    }

    @Override
    public MD360Director getMD360DirectorPlus() {
        return new OrthogonalDirectorPlus(new MD360Director.Builder());
    }

    @Override
    public MDPosition getModelPositionPlus() {
        return positionPlus;
    }

    private class OrthogonalDirectorPlus extends MD360Director {

        private OrthogonalDirectorPlus(Builder builder) {
            super(builder);
        }

        @Override
        public void setDeltaX(float mDeltaX) {
            // nop
        }

        @Override
        public void setDeltaY(float mDeltaY) {
            // nop
        }

        @Override
        public void updateSensorMatrix(float[] sensorMatrix) {
            // nop
        }

        @Override
        protected void updateProjection() {
            planeScaleCalculator.setViewportRatio(getRatio());
            planeScaleCalculator.calculate();
            final float left = -planeScaleCalculator.getViewportWidth() / 2;
            final float right = planeScaleCalculator.getViewportWidth() / 2;
            final float bottom = -planeScaleCalculator.getViewportHeight() / 2;
            final float top = planeScaleCalculator.getViewportHeight() / 2;
            final float far = 500;
            Matrix.orthoM(getProjectionMatrix(), 0, left, right, bottom, top, getNear(), far);
        }
    }

}
